﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;

namespace BasicForms
{
    public partial class FrmCauHinhTenMayTinh : BasicForms.FrmOKCancel
    {
        public FrmCauHinhTenMayTinh()
        {
            InitializeComponent();
        }

        private void FrmCauHinhTenMayTinh_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            txtTenMayTinh.Focus();

            MayTinh mayTinh = DanhMucMayTinh.Instance.GetByHardwareID(GeneralLibs.LocalSettings.HeThong.HardwareID);
            if (mayTinh == null)
            {
                mayTinh = DanhMucMayTinh.Instance.NewDoiTuong();
                mayTinh.HardwareID = GeneralLibs.LocalSettings.HeThong.HardwareID;
                mayTinh.Ten = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                mayTinh.InsertToDB();

                //GeneralLibs.LocalSettings.HeThong.MayTinhID = mayTinh.MayTinhID;

                GeneralLibs.LocalSettings.HeThong.Save2File();
            }

            txtMayTinhID.Text = mayTinh.ID.ToString();
            txtTenMayTinh.Text = mayTinh.Ten;

            txtTenCuaHang.Text = GeneralLibs.LocalSettings.HeThong.TenCuaHang;
            txtDiaChi.Text = GeneralLibs.LocalSettings.HeThong.DiaChi;
            txtDienThoai.Text = GeneralLibs.LocalSettings.HeThong.DienThoai;
            GeneralLibs.LocalSettings.HeThong.Save2File();
        }

        protected override void btnOK_Click(object sender, EventArgs e)
        {
            MayTinh mayTinh = DanhMucMayTinh.Instance.GetByHardwareID(GeneralLibs.LocalSettings.HeThong.HardwareID);
            if (mayTinh == null)
            {
                mayTinh = DanhMucMayTinh.Instance.NewDoiTuong();
                mayTinh.HardwareID = GeneralLibs.LocalSettings.HeThong.HardwareID;
                mayTinh.Ten = txtTenMayTinh.Text;
                mayTinh.InsertToDB();
            }
            else{
                mayTinh.Ten = txtTenMayTinh.Text;
                mayTinh.UpdateToDB();
            }

            GeneralLibs.LocalSettings.HeThong.TenCuaHang = txtTenCuaHang.Text;
            GeneralLibs.LocalSettings.HeThong.DiaChi = txtDiaChi.Text;
            GeneralLibs.LocalSettings.HeThong.DienThoai = txtDienThoai.Text;
            GeneralLibs.LocalSettings.HeThong.Save2File();

            //if (txtTenMayTinh.Text.Length < 2)
            //{
            //    MessageBox.Show("Tên máy tính cần ít nhất 2 ký tự");
            //}
            //string ma = txtTenMayTinh.Text.Substring(0,2);
            //string ten = txtTenMayTinh.Text.Substring(2);

            //if (txtTenMayTinh.Text == GeneralLibs.LocalSettings.HeThong.TenMay)
            //{
            //}
            //else
            //{
            //    if (DanhMucMayTinh.Instance.GetByProperty("Ma", ma) != null)
            //    {
            //        MessageBox.Show("Hai ký tự đầu tiên của tên máy đã có trong hệ thống");
            //        txtTenMayTinh.Focus();
            //        return;
            //    }
            //    else
            //    {
            //        MayTinh mayTinh = DanhMucMayTinh.Instance.NewDoiTuong();
            //       // mayTinh.Ma = ma;
            //        mayTinh.Ten = ten;
            //        mayTinh.InsertToDB();

            //        GeneralLibs.LocalSettings.HeThong.TenMay = txtTenMayTinh.EditValue as string;
            //        GeneralLibs.LocalSettings.HeThong.Save2File();

            //    }
            //}
            //if((txtTenMayTinh.EditValue is string) && (string)txtTenMayTinh.EditValue != "")
            //    GeneralLibs.ThongSoMacDinhContainer.AddTenMayNeuChuaCo(txtTenMayTinh.EditValue as string);

            this.Close();
        }
    }
}
